package org.springframework.beans.factory.xml;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class BeanDefinitionParserDelegate {

    public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans";
    
    private static final String DEFAULT_MERGE_ATTRIBUTE = "default-merge"; // 根元素beans
                                                                           // 的属性

    public static final String DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire"; // 根元素beans
                                                                                 // 的属性自动装配

    public static final String DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE = "default-autowire-candidates"; // 根元素beans
                                                                                                       // 的属性自动装配不考虑的bean

    public static final String DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency_check"; // 根元素beans
                                                                                                 // 的属性自动装配不考虑的bean

    public static final String DEFAULT_DESTROY_METHOD_ATTRIBUTE = "default-destroy-method"; // 根元素beans
                                                                                             // 的属性自动装配不考虑的bean

    public static final String DEFAULT_INIT_METHOD_ATTRIBUTE = "default-init-method"; // 根元素beans
                                                                                       // 的属性自动装配不考虑的bean

    public static final String DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init"; // 根元素beans
                                                                                   // 的属性自动装配不考虑的bean

    public static final String BEAN_ELEMENT = "bean";

    private static final String ID_ATTRIBUTE = "id";

    private static final String NAME_ATTRIBUTE = "name";


    private static final String BEAN_NAME_DELIMITERS = ",; ";
    
    private final XmlReaderContext readerContext;

    private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();

    public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
        this.readerContext = readerContext;
    }

    public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {
        String id = ele.getAttribute(ID_ATTRIBUTE);
        String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);

        List<String> aliases = new ArrayList<String>();
        if (StringUtils.hasLength(nameAttr)) {
            String[] nameArr = StringUtils.tokenizeToStringArray(id, BEAN_NAME_DELIMITERS);
            aliases.addAll(Arrays.asList(nameArr));
        }

        String beanName = id;
        if (containingBean == null) {
            checkNameUniqueness(beanName, aliases, ele);
        }
        return null;
    }
    
    public void initDefaults(Element root) {
        populateDefaults(this.defaults, root);
        this.readerContext.fireDefaultsRegistered(this.defaults);
    }

    /**
     * 对beans 元素 进行设置
     * @param defaults
     * @param root
     */
    private void populateDefaults(DocumentDefaultsDefinition defaults,
            Element root) {
        defaults.setMerge(root.getAttribute(DEFAULT_MERGE_ATTRIBUTE));
        defaults.setAutowire(root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE));
        defaults.setDependencyCheck(root.getAttribute(DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE));
        defaults.setLazyInit(root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE));
        if (root.hasAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)) {
            defaults.setAutowireCandidates(root.getAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE));
        }

        if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) {
            defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE));
        }
        if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) {
            defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE));
        }
        defaults.setSource(readerContext);
    }

    public String getNamespaceURI(Node node) {
        return node.getNamespaceURI();
    }

    public boolean isDefaultNamespace(String namespaceUri) {
        return !StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri);
    }

    public boolean nodeNameEquals(Node node, String desiredName) {
        return desiredName.equals(node.getNodeName()) || desiredName.equals(getLocalName(node));
    }

    private String getLocalName(Node node) {
        return node.getLocalName();
    }

    private void checkNameUniqueness(String beanName, List<String> aliases, Element ele) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
